#include"RingQueue.hpp"
#include<pthread.h>
#include<unistd.h>
#include"task.hpp"
#include<ctime>
void *Productor(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);
    int cnt=5;
    while(true)
    {
        int data1 = rand() % 10; // [1, 10] // 将来深刻理解生产消费，就要从这里入手，TODO
        usleep(rand()%123);
        int data2 = rand() % 10; // [1, 10] // 将来深刻理解生产消费，就要从这里入手，TODO
        usleep(rand()%123);
        char oper = opers[rand() % (opers.size())];
        Task t(data1, data2, oper);
        std::cout << "productor task: " << t.PrintTask() << std::endl;
        rq->Push(t);
        sleep(1);
    }
}
void*Consumer(void*args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        Task t;
        rq->Pop(&t);
        t();
        std::cout<<"Consumer done,data is"<<t.PrintResult()<<std::endl;
    }

}
int main()
{
    srand((u_int64_t)(nullptr)^pthread_self());
    pthread_t p[2],c[3];
    RingQueue<Task>*rq=new RingQueue<Task>();
    pthread_create(&p[0],nullptr,Productor,rq);
    pthread_create(&p[1],nullptr,Productor,rq);
    pthread_create(&c[0],nullptr,Consumer,rq);
    pthread_create(&c[1],nullptr,Consumer,rq);
    pthread_create(&c[2],nullptr,Consumer,rq);
    pthread_join(p[0],nullptr);
    pthread_join(p[1],nullptr);
    pthread_join(c[0],nullptr);
    pthread_join(c[1],nullptr);
    pthread_join(c[2],nullptr);
    return 0;
}